﻿// *---------------------------------------------------------------------------------------------------------*
// Solution:         JwikIDE									
// Project:           JwiKIDE								
// Source File:    ClassLoader.cs									
// *---------------------------------------------------------------------------------------------------------*
// 
// © 2012 Digital Six Labs, LLC All Rights Reserved
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// 
// 
// 
namespace JwiKIDE.JavaClassFileClasses
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class ClassLoader
    {
        public static List<ClassInfo> Classes{ get; private set; }

        public static string ClassPath { get; set; }

        static ClassLoader()
        {
            Classes = new List<ClassInfo>();
        }

        public static void Load(string name)
        {
            var newClass = new ClassInfo();
            
            newClass.LoadClassFile(ClassPath+"\\"+name);
            Classes.Add(newClass);
            newClass.ConstantPool.ResolveMethodReferences();
            
            
        }

        public static UInt16 TotalMethodCount
        {
            get
            {
                UInt16 cnt = 0;
                foreach (ClassInfo css in Classes) cnt += (ushort)css.Methods.Count;
                return cnt;
            }
        }

        public static int TotalConstantCount
        {
            get
            {
                byte cnt = 0;
                foreach (ClassInfo css in Classes) cnt += (byte)css.ConstantPool.TotalConstantEntries(); //.ConstantPoolEntries.Count;
                return cnt;
            }
        }

        public static int TotalStringCount
        {
            get
            {
                byte cnt = 0;
                foreach (ClassInfo css in Classes)
                    foreach (ConstantPoolEntry ce in css.ConstantPool.ConstantPoolEntries)
                        if (ce.TypeCode == ConstantTypes.String) cnt++;
                return cnt;
            }
        }

        public static ushort TotalStringSize
        {
            get
            {
                ushort cnt = 0;
                foreach (ClassInfo css in Classes)
                    foreach (ConstantPoolEntry ce in css.ConstantPool.ConstantPoolEntries)
                        if (ce.TypeCode == ConstantTypes.String)
                        {
                            cnt+=(ushort)(css.ConstantPool.ConstantPoolEntries[ce.Index1].StringValue.Length+1);
                        }
                return cnt;
            }
        }

        public static ushort TotalStaticFields
        {
            get
            {
                ushort cnt = 0;
                foreach (ClassInfo css in Classes) cnt += css.StaticFieldCount;
                return cnt;
            }
        }

        internal static int GetClassIndex(string cName)
        {
            for (int i = 0; i < Classes.Count; i++)
                if (Classes[i].ClassName == cName) return i;
            return -1;
        }

        internal static int GetClassIndex(int index)
        {
            int i = 0;

            // search through all classes
            while (index >= Classes[i].Methods.Count)
                index -= Classes[i++].Methods.Count;

            return i;
        }
        internal static int GetConstantEntry(int index)
        {
            int i = 0;

            // search through all classes
            while (index >= Classes[i].ConstantPool.ConstantPoolEntries.Count)
                index -= Classes[i++].ConstantPool.ConstantPoolEntries.Count;

            return Classes[i].ConstantPool.GetConstantEntry(index);
        }

        internal static string GetString(int index)
        {
            int i = 0;

            // search through all classes
            while (index >= Classes[i].ConstantPool.TotalStrings)
                index -= Classes[i++].ConstantPool.TotalStrings;
            return Classes[i].ConstantPool.GetString(index);
        }

        internal static MethodInfo GetMethod(int index)
        {
            int i = 0;

            // search through all classes
            while (index >= Classes[i].Methods.Count) index -= Classes[i++].Methods.Count;

            return (Classes[i].Methods[index]);
        }

        internal static ClassInfo GetClassInfoFromMethodIndex(int index)
        {
            int classIndex = 0;

            // search through all classes
            while (index >= Classes[classIndex].Methods.Count)
                index -= Classes[classIndex++].Methods.Count;

            return Classes[classIndex];
        }

        internal static bool FieldExists(string className, string fieldName, string fieldType)
        {
            while (className != null)
            {
                if (FieldExistsExact(className, fieldName, fieldType))
                    return true;
                className = GetSuperClassName(className);
            }
            return false;
        }

        internal static string GetSuperClassName(string className)
        {
            // search through all classes
            for (int i = 0; i < Classes.Count; i++)
            {
                ClassInfo classInfo = Classes[i];

                if (classInfo.Name == className) return classInfo.SuperClassName;
            }
            return null;
        }

        private static bool FieldExistsExact(string className, string fieldName, string fieldType)
        {
            // search through all classes
            for (int i = 0; i < Classes.Count; i++)
            {
                ClassInfo classInfo = Classes[i];

                if ((classInfo.ClassName == className) &&
                    (classInfo.ProvidesField(fieldName, fieldType)))
                    return true;
            }
            return false;
        }

        internal static FieldInfo GetFieldInfo(string className, string name, string type)
        {
            while (className != null)
            {
                FieldInfo result = GetFieldInfoExact(className, name, type);
                if (result != null)
                    return result;
                className = GetSuperClassName(className);
            }
            return null;
        }

        private static FieldInfo GetFieldInfoExact(string className, string name, string type)
        {
            // search through all classes
            for (int i = 0; i < Classes.Count; i++)
            {
                ClassInfo classInfo = Classes[i];

                if (classInfo.ClassName==className)
                    return classInfo.GetFieldInfo(name, type);
            }
            return null;
        }

        internal static int GetStaticFieldIndex(string className, string name, string type)
        {
            // search through all classes
            for (int i = 0, cnt = 0; i < Classes.Count; i++)
            {
                ClassInfo classInfo = Classes[i];

                if (classInfo.Name ==className)
                    return cnt + classInfo.GetFieldIndex(AccessTypeFlags.STATIC, name, type);
                else
                    cnt += classInfo.StaticFieldCount;
            }
            return -1;
        }

        internal static ClassInfo GetClassInfo(string className)
        {
            // search through all classes
            for (int i = 0; i < Classes.Count; i++)
            {
                if (Classes[i].Name==className)
                    return Classes[i];
            }

            return null;
        }

        internal static int GetMethodIndex(string className, string name, string type)
        {
            int index = 0;
            for (int i = 0; i < Classes.Count; i++)
            {
                if (Classes[i].Name==className
                    ||Classes[i].ClassName==className)
                {
                    index += Classes[i].GetMethodIndex(name, type);
                    return index;
                }
                else index += Classes[i].Methods.Count;
            }
            return -1;
        }

        internal static void Clear()
        {
            Classes.Clear();
        }
    }
}
